<?php
session_start();

class c_admin extends CI_Controller{
    
    private $data;
    
    public function __construct(){
        parent::__construct();
        $this->load->helper("url");
        $this->load->model('m_model');
    }
    
    public function index(){
        $checkLogin = $this->checkIsLogin();
        if($checkLogin == false){
            echo "<script>window.location.href='/st_login'</script>";
            return;
        }

    	$this->data["page_name"] = "v_admin_content";
        $this->renderListNews();
        $this->load->view("v_admin_home.php", $this->data);
    }
    
    public function renderListNews(){
        $pageSize = 10;
        $pageIndex = isset($_GET["page"]) ? $_GET["page"] : 1;
        list($count, $listNews) = $this->m_model->get_list_news(($pageIndex - 1) * $pageSize, $pageSize, -1);
        $this->data["listNews"] = $listNews;
        $this->data["pageIndex"] = $pageIndex;
        $numberOfPage = ceil($count / $pageSize);
        $this->paging("/st_admin", $numberOfPage, $pageIndex);
    }

    public function add_new(){
        $this->checkIsLogin();
        
        if(isset($_POST["action"]) && $_POST["action"] === "true"){
            $title = $_POST['title'];
            $shortDesc = $_POST['shortDesc'];
            $longDesc = $_POST['longDesc'];
            $tag = $_POST['tag'];
            $status = $_POST['status'];
            $imgThumb = $_POST['imgThumb'];
            $result = $this->m_model->insert_news($title, $shortDesc, $longDesc, $tag, $status, $imgThumb);
            echo $result;
        }
        else{
            $this->data["page_name"] = "v_admin_add_new";
            $this->load->view("v_admin_home.php", $this->data);
        }
    }
    
    public function update_news(){
        $this->checkIsLogin();
        
        if(isset($_POST["action"]) && $_POST["action"] === "true"){
            $title = $_POST['title'];
            $shortDesc = $_POST['shortDesc'];
            $longDesc = $_POST['longDesc'];
            $tag = $_POST['tag'];
            $status = $_POST['status'];
            $imgThumb = $_POST['imgThumb'];
            $newsId = $_POST['newsId'];
            $result = $this->m_model->update_news($title, $shortDesc, $longDesc, $tag, $status, $imgThumb, $newsId);
            echo $result;
        }
        else{
            $newsId = isset($_GET['id']) ? $_GET['id'] : 0;
            $result = $this->m_model->getNewsById($newsId);
            $this->data['update'] = $result;

            $this->data["page_name"] = "v_admin_add_new";
            $this->load->view("v_admin_home.php", $this->data);
        }
    }
    
    public function delete_news(){
        $newsId = isset($_GET['id']) ? $_GET['id'] : 0;
        $result = $this->m_model->delete_news($newsId);
        echo "<script type='text/javascript'>window.location.href='/st_admin'</script>";
    }
    
    public function video(){
        $this->checkIsLogin();
        $pageSize = 20; 
        $pageIndex = isset($_GET['page']) ? $_GET['page'] : 1;
        $result = $this->m_model->getListVideoByPage($pageIndex - 1, $pageSize);
        $numberOfPage = ceil(count($result) / $pageSize);
        
        $this->data["pageIndex"] = $pageIndex;
        $this->data["list_video"] = $result;
        $this->data["page_name"] = "v_admin_video_list";
        $this->paging("/st_video", $numberOfPage, $pageIndex);
        
        $this->load->view("v_admin_home.php", $this->data);
    }
    
    public function add_video(){
        if(isset($_POST['action']) && $_POST['action'] == true){
            $title = $_POST['title'];
            $image = $_POST['image'];
            $link = $_POST['link'];
            $result = $this->m_model->insertVideo($title, $image, $link);
            echo $result;
        }
        else{
            $this->data["page_name"] = "v_admin_add_video";
            $this->load->view("v_admin_home.php", $this->data);
        }
    }
    
    public function delete_video(){
        $videoId = isset($_GET['id']) ? $_GET['id'] : 0;
        $result = $this->m_model->delete_video($videoId);
        echo "<script type='text/javascript'>window.location.href='/st_video'</script>";
    }

    public function upload(){
        /*
         * -1: file invalid
         */
        
        $validextensions = array("jpeg", "jpg", "png");
        $temporary = explode(".", $_FILES["fileImageUpload"]["name"]);
        $file_extension = end($temporary);
        if ((($_FILES["fileImageUpload"]["type"] == "image/png") || ($_FILES["fileImageUpload"]["type"] == "image/jpg") 
                || ($_FILES["fileImageUpload"]["type"] == "image/jpeg")) && in_array($file_extension, $validextensions)){
            $date = new DateTime();
            $fileName = $date->getTimestamp() . "_" . $_FILES["fileImageUpload"]["name"];
            move_uploaded_file($_FILES["fileImageUpload"]["tmp_name"], UPLOAD . $fileName);
            echo $fileName;
        }
        else{
            echo -1;
        }
    }

    private function checkIsLogin(){
        if(isset($_SESSION['username'])){
            return true;
        }
        else{
            echo "<script>window.location.href = '/st_login';</script>";
            return false;
        }   
    }

    private function paging($pathInfo, $numberOfPage, $currentPage){
        $prevPage = $currentPage == 1 ? 1 : $currentPage - 1;
        $nextPage = $currentPage == $numberOfPage ? $numberOfPage : $currentPage + 1;
        $lastPage = $numberOfPage;

        $html = "<a href='" . $pathInfo . "?page=1' class='currentpage' style='color: black;''>Trang đầu</a>"
            . "<a href='" . $pathInfo . "?page=" . $prevPage . "' class='currentpage' style='color: black;''>Trang trước</a>";
        for($i=1; $i<=$numberOfPage; $i++){
            if($i == $currentPage){
                $html .= "<span class='currentpage'>" . $i . "</span>";
            }
            else{
                $html .= "<a href='" . $pathInfo . "?page=" . $i . "' class='currentpage' style='color: black;''>" . $i . "</a>";
            }
        }
        $html .= "<a href='" . $pathInfo . "?page=" . $nextPage . "' class='currentpage' style='color: black;''>Trang sau</a>"
            . "<a href='" . $pathInfo . "?page=" . $lastPage . "' class='currentpage' style='color: black;''>Trang cuối</a>";

        $this->data["paging"] = $html;
    }
    
}